package com.vogue.supplymall.order.domain.biz.impl;

import com.vogue.supplymall.common.constant.ConstantBean;
import com.vogue.supplymall.common.domain.dto.OrderDetailInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.tool.VestUtils;
import com.vogue.supplymall.order.domain.biz.OrderUpdateBiz;
import com.vogue.supplymall.order.domain.biz.TimeoutTaskProcessorBiz;
import com.vogue.supplymall.order.domain.model.Account;
import com.vogue.supplymall.order.domain.model.Order;
import com.vogue.supplymall.order.domain.model.OrderBuyer;
import com.vogue.supplymall.order.domain.service.AccountService;
import com.vogue.supplymall.order.domain.service.OrderBuyerService;
import com.vogue.supplymall.order.domain.service.OrderService;
import com.vogue.supplymall.order.domain.service.OrderStatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Timeout task processor biz.
 *
 * @author zhangliang
 * Create in 2018-09-19
 */
@Service("timeoutTaskProcessorBiz")
public class TimeoutTaskProcessorBizImpl implements TimeoutTaskProcessorBiz {

    public static final Logger LOGGER_TIMEOUTTASKPROCESSORBIZIMPL = LoggerFactory.getLogger(TimeoutTaskProcessorBizImpl.class);
    @Resource
    private OrderService orderService;
    @Resource
    private OrderStatusService orderStatusService;
    @Resource
    private OrderBuyerService orderBuyerService;
    @Resource
    private AccountService accountService;
    @Resource
    private OrderUpdateBiz orderUpdateBiz;

    private static final String SELECT_TIMEOUT_RECEIVING = "select o.id oid,o.syscodeid,os.id osid,os.orderstatus from `order` o "
            + " INNER JOIN order_status os on(o.id = os.orderid)"
            + " where os.orderstatus = 3 and TIMESTAMPDIFF(SECOND,os.modifydate,now()) >= "
            + (ConstantBean.CONFIRM_RECEIVE_TIME.longValue() / 1000L)
            + " ORDER BY os.modifydate desc ";

    @Override
    public void updateTimeoutReceiving() {
        List<Map<String, Object>> waitReceings = orderService.query(SELECT_TIMEOUT_RECEIVING);
        OrderDetailInfo orderDetailInfo = new OrderDetailInfo();
        for (Map<String, Object> objectMap : waitReceings) {
            // 查询订单信息
            Order order = orderService.get(new Long(objectMap.get("oid").toString()));
            if (null == order) {
                LOGGER_TIMEOUTTASKPROCESSORBIZIMPL.error("30天自动收货，订单信息不存在");
                continue;
            }
            // 查询订单状态信息
//            OrderStatus status = orderStatusService.get(new Long(objectMap.get("osid").toString()));
            Map<String, String> para = new HashMap<>();
            para.put("orderid", order.getId().toString());
            para.put("syscodeid", order.getSyscodeid().toString());
            // 查询订单买家信息
            OrderBuyer orderBuyer = orderBuyerService.selectOneByWhr0("orderid = #{orderid} and syscodeid = #{syscodeid}", para);
            if (null == orderBuyer) {
                LOGGER_TIMEOUTTASKPROCESSORBIZIMPL.error("30天自动收货，订单买家信息不存在");
                continue;
            }
            para.put("organid", orderBuyer.getOrganid().toString());
            para.put("matchid", orderBuyer.getMatchid());
            // 查询订单买家账户
            Account account = accountService.selectOneByWhr0("syscodeid = #{syscodeid} and organid = #{organid} and matchid = #{matchid}", para);
            if (null == account) {
                LOGGER_TIMEOUTTASKPROCESSORBIZIMPL.error("30天自动收货，订单买家账户信息不存在");
            }
            UserInfo userInfo = new UserInfo();
            userInfo.setSyscodeid(order.getSyscodeid());
            if (null != account) {
                userInfo.setAccountid(account.getId());
            }
            orderDetailInfo.setOrderid(new Long(objectMap.get("oid").toString()));
            orderDetailInfo.setOrderstatus(Integer.valueOf(objectMap.get("orderstatus").toString()));
            orderDetailInfo.setStatusid(new Long(objectMap.get("osid").toString()));
            orderDetailInfo.setAction(4);
            VestUtils.setTendid(order.getTendid().toString());
            // 更新订单状态
            try {
                orderUpdateBiz.updateStatus(orderDetailInfo, userInfo);
            } catch (RuntimeException e) {
                LOGGER_TIMEOUTTASKPROCESSORBIZIMPL.error(e.getMessage());
                continue;
            }
//            // 自动退处理
//            orderUpdateBiz.orderReceiving(order, status, userInfo);
        }
    }
}
